package com.yanggeng.demo.beginnerguidedemo.activity;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.amap.api.services.weather.LocalWeatherForecastResult;
import com.amap.api.services.weather.LocalWeatherLive;
import com.amap.api.services.weather.LocalWeatherLiveResult;
import com.amap.api.services.weather.WeatherSearch;
import com.amap.api.services.weather.WeatherSearchQuery;
import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.TestApplication;
import com.yanggeng.demo.beginnerguidedemo.activity.BaseActivity;
import com.yanggeng.demo.beginnerguidedemo.adapter.SimpleAdapter;
import com.yanggeng.demo.beginnerguidedemo.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 天气
 * Created by yanggeng on 2017/10/11.
 */

public class WeatherActivity extends BaseActivity implements WeatherSearch.OnWeatherSearchListener {

    private TextView tvCity, tvTime, tvWeather, tvTemperature, tvWindType, tvWindLevel, tvHumidity;

    private WeatherSearchQuery searchQuery;
    private WeatherSearch mWeatherSearch;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        setTitle(true, "天气查询", R.mipmap.c_home_setting_icon);
        tvCity = (TextView) findViewById(R.id.tv_city);
        tvTime = (TextView) findViewById(R.id.tv_time);
        tvWeather = (TextView) findViewById(R.id.tv_weather);
        tvTemperature = (TextView) findViewById(R.id.tv_temperature);
        tvWindType = (TextView) findViewById(R.id.tv_wind_type);
        tvWindLevel = (TextView) findViewById(R.id.tv_wind_level);
        tvHumidity = (TextView) findViewById(R.id.tv_humidity);
        tvCity.setText(TestApplication.getApplication().getCity());
        searchQuery = new WeatherSearchQuery(TestApplication.getApplication().getCity(), WeatherSearchQuery.WEATHER_TYPE_LIVE);
        mWeatherSearch = new WeatherSearch(this);
        mWeatherSearch.setOnWeatherSearchListener(this);
        mWeatherSearch.setQuery(searchQuery);
        mWeatherSearch.searchWeatherAsyn();

//        getToolbar().setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
//            @Override
//            public boolean onMenuItemClick(MenuItem item) {
//                switch (item.getItemId()) {
//                    case R.id.item1:
//                        ToastUtil.centerToast(context, "点击了item1");
//                        break;
//                    case R.id.item2:
//                        ToastUtil.centerToast(context, "点击了item2");
//                        break;
//                    case R.id.item3:
//                        ToastUtil.centerToast(context, "点击了item3");
//                        break;
//                }
//                return true;
//            }
//        });
    }

    @Override
    public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int i) {
        if (i == 1000) {
            if (localWeatherLiveResult != null && localWeatherLiveResult.getLiveResult() != null) {
                LocalWeatherLive weatherLive = localWeatherLiveResult.getLiveResult();
                tvTime.setText(weatherLive.getReportTime() + "发布");
                tvWeather.setText(weatherLive.getWeather());
                tvTemperature.setText(weatherLive.getTemperature() + "℃");
                tvWindType.setText(weatherLive.getWindDirection() + "风");
                tvWindLevel.setText(weatherLive.getWindPower() + "级");
                tvHumidity.setText(weatherLive.getHumidity() + "%");

            }
        }
    }

    @Override
    public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int i) {

    }

    @Override
    protected void navigationOnclick(int paramId) {
        super.navigationOnclick(paramId);
        if (paramId == RIGHT_BUTTON) {
            if (popupWindow != null && popupWindow.isShowing()) {
                closeWindow();
            } else {
                showPopupWindow(iv_right);
            }
        }
    }

    //    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu_car_list, menu);
//        return true;
//    }

    private PopupWindow popupWindow;

    public void showPopupWindow(View anchorView) {
        View contentView = View.inflate(context, R.layout.layout_recycleview, null);
        RecyclerView rv = (RecyclerView) contentView.findViewById(R.id.recycle_view);
        List<String> data = new ArrayList<>();
        data.add("京A12345");
        data.add("冀B22445");
        data.add("晋M22M12");
        rv.setLayoutManager(new LinearLayoutManager(context, OrientationHelper.VERTICAL, false));
        SimpleAdapter simpleAdapter = new SimpleAdapter(context, data);
        simpleAdapter.setOnItemClickListener(new SimpleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                ToastUtil.centerToast(context, "点击了第" + position + "个按钮");
                finish();
            }
        });
        rv.setAdapter(simpleAdapter);
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setAnimationStyle(R.style.tips_anim);
        popupWindow.setBackgroundDrawable(new ColorDrawable());
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    popupWindow.dismiss();
                    return true;
                }
                return false;
            }
        });
        int[] location = new int[2];
        anchorView.getLocationOnScreen(location);
//        popupWindow.showAsDropDown(showAtView, 50, 0);
//        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int windowWidth = contentView.getMeasuredWidth();
        int windowHeight = contentView.getMeasuredHeight();
        int anchorHeight = anchorView.getHeight();
        int anchorWidth = anchorView.getWidth();

        int x = 0, y = 0;

        Log.e("location", "x = " + x + ", y = " + y);
//        popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, x, y);

        popupWindow.showAsDropDown(anchorView);
    }

    private void closeWindow() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }
}
